package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.RetardRecord;
import com.example.elderlycare.dao.mapper.RetardRecordMapper;
import com.example.elderlycare.service.RetardRecordService;
import com.example.elderlycare.service.entity.RetardRecordItem;
import com.example.elderlycare.service.entity.RetardRecordListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RetardRecordServiceImpl extends ServiceImpl<RetardRecordMapper, RetardRecord>
        implements RetardRecordService {

    @Resource
    RetardRecordMapper retardRecordMapper;

    @Override
    public RetardRecordListResponseBody queryRetardRecords(Map<String, Object> params) {
        List<RetardRecordItem> retardRecordItemList = new ArrayList<>();
        List<RetardRecord> retardRecords = retardRecordMapper.queryRetardRecords(params);
        for (RetardRecord retardRecord : retardRecords) {
            RetardRecordItem item = RetardRecordItem.builder()
                    .id(retardRecord.getId())
                    .elderName(retardRecord.getElder().getName())
                    .elderNumber(retardRecord.getElder().getNumber())
                    .elderCertificate(retardRecord.getElder().getCertificateName())
                    .evaluation(retardRecord.getEvaluation())
                    .retardDate(retardRecord.getRetardDate())
                    .reason(retardRecord.getReason())
                    .build();
            retardRecordItemList.add(item);
        }
        int total = retardRecordMapper.getRetardRecordsCount(params);
        return RetardRecordListResponseBody.builder()
                .retardRecordItemList(retardRecordItemList)
                .total(total)
                .build();
    }
}
